package com.eight.controller;

import com.eight.annotation.Login;
import com.eight.service.UserService;
import com.eight.vo.ComResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.session.SessionRegistry;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: TODO
 * @Author: Van.Planifolia
 * @CreateTime: 2023-02-27  19:39
 * @Version: 1.0
 */
@RestController
public class AdminLogin {
    @Autowired
    SessionRegistry sessionRegistry;
    @Autowired
    private UserService userService;

    /**
     * 登录的Aop爬架方法
     * @param statu 登录的状态
     * @param token 登录成功的token
     */
    @Login
    public void loginAop(int statu,String token){}

    /**
     * 获取当前登录的用户人数
     * @return 登录的用户人数
     */
    @GetMapping("/login/getonline")
    public ComResult getOnline(){
        // 获取用户对象的List并且返回给前端
        List<User> userList = sessionRegistry.getAllPrincipals().stream().map(e->(User) e).collect(Collectors.toList());
        return new ComResult(2000,"查询成功",userList);
    }

    /**
     * 用户登出操作
     * @param request 请求对象
     * @return 登出结果
     */
    @GetMapping("/login/logout")
    public ComResult adminLogOut(HttpServletRequest request){
       return userService.logout(request);
    }
}
